package vmo;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import shared.Vec2i;

public class Chunk {

	public Tile [][][] m_tiles;
	public Vec2i m_position;
	
	public Chunk(boolean make)
	{
	
		m_tiles=new Tile[16][][];
		if (make)
		{
			for (int k=0;k<16;k++)
			{
				m_tiles[k]=new Tile [16][];
				for (int i=0;i<16;i++)
				{
	
					m_tiles[k][i]=new Tile[16];
					for (int j=0;j<16;j++)
					{
						
					}
					
				}
			}
		}	
	}
	
	public Chunk(DataInputStream stream) throws IOException
	{
		int x=stream.readInt(); int y=stream.readInt();
		m_position=new Vec2i(x,y);

		m_tiles=new Tile[16][][];	
		for (int k=0;k<16;k++)
		{
			m_tiles[k]=new Tile [16][];
			for (int i=0;i<16;i++)
			{

				m_tiles[k][i]=new Tile[16];
				for (int j=0;j<16;j++)
				{
					m_tiles[k][i][j]=new Tile(stream);
				}
			}
		}
				
	}
	
	public void Save(DataOutputStream stream) throws IOException
	{
		//write position values
		stream.writeInt(m_position.x);
		stream.writeInt(m_position.y);
		//write tiles
		for (int i=0;i<16;i++)
		{
			for (int j=0;j<16;j++)
			{
				for (int k=0;k<16;k++)
				{
					//write tile
					m_tiles[i][j][k].Save(stream);
				}
			}
			
		}	
	}
	
	
	public Chunk Clone()
	{
		Chunk c=new Chunk(true);
		c.m_position=m_position;
		for (int i=0;i<16;i++)
		{
			for (int j=0;j<16;j++)
			{
				for (int k=0;k<16;k++)
				{
					c.m_tiles[i][j][k]=m_tiles[i][j][k];
				}
			}
		
		}
		return c;
	}
	
	public void BatchsetImage()
	{
		for (int i=0;i<16;i++)
		{
			if (m_tiles[i]!=null)
			{
				for (int j=0;j<16;j++)
				{
					for (int k=0;k<16;k++)
					{
						if (m_tiles[i][j][k]!=null)
						{
							m_tiles[i][j][k].CalcImageNum();
						}
						
					}
					
				}
			}
		}		
	}
}
